from typing import Dict, Type
from .base_vehicle_controller import BaseVehicleController

class ControllerRegistry:
    _controllers: Dict[str, Type[BaseVehicleController]] = {}

    @classmethod
    def register(cls, controller_type: str):
        """控制器注册装饰器

        Args:
            controller_type: 控制器类型名称
        """
        def decorator(controller_class: Type[BaseVehicleController]):
            if not issubclass(controller_class, BaseVehicleController):
                raise TypeError("控制器必须继承自BaseVehicleController")
            cls._controllers[controller_type] = controller_class
            return controller_class
        return decorator

    @classmethod
    def get_controller(cls, controller_type: str) -> Type[BaseVehicleController]:
        """获取指定类型的控制器类

        Args:
            controller_type: 控制器类型名称

        Returns:
            控制器类

        Raises:
            ValueError: 当控制器类型不存在时
        """
        controller_class = cls._controllers.get(controller_type)
        if not controller_class:
            raise ValueError(f"不支持的控制器类型: {controller_type}")
        return controller_class

    @classmethod
    def list_controllers(cls) -> list[str]:
        """获取所有支持的控制器类型列表"""
        return list(cls._controllers.keys())